/**
 * @description plugin
 * @author wangfupeng
 */

import { DomEditor } from '@wangeditor/editor'

function withCtrlEnter(editor) {
    const { insertNode, move } = editor
    const newEditor = editor

    const node1 = { type: 'question', children: [{ text: 'aaa',}] }

    setTimeout(() => {
        // beforeInput 事件不能识别 ctrl+enter ，所以自己绑定 DOM 事件
        const { $textArea } = DomEditor.getTextarea(newEditor)
        if ($textArea == null) return
        $textArea.on('keydown', e => {
            // KeyboardEvent
            const event = e
            const isCtrl = event.ctrlKey || event.metaKey
            // console.log(isCtrl)
            if (event.key === 'Enter' && isCtrl) {
                // ctrl+enter 触发换行
                newEditor.insertBreak()
            }
        })
    })

    newEditor.insertBreak = () => {
        const event = window.event
        const isCtrl = event.ctrlKey || event.metaKey
        // 只有 ctrl 才能换行
        if (isCtrl) {
            console.log('Enter')
            // insertNode(node1)
            // move(111111)
            // insertBreak()
        }
    }
    return newEditor
}

export default withCtrlEnter
